////////////////////////////////////////
// StringTableTester.cpp
/* Name: Stephen Durfey
 * Class: cs335
 * Prof: Dr. Michael Poor
 * Due Date: October 17, 2008
 * Description: This program acts as a Database Management System that holds
 * a list of cities with a small sample of its characteristics and location. 
 * Once the database has been created, the user will be prompted to enter
 * a query that will use one of four relational operators when searching through
 * the database (equal (==), not equal(!=), greater than (>), and less than (<).
 * If a match is found in the appropriate column then the program will print out
 * the columns for the stored record as entered by the user during the initial
 * query form. 
 ******************************************************************************/

#include <iostream>
#include <fstream>
#include "StringTable.h"
using namespace std;

void compareRelOp(StringTable &table, vector<int> &vectInt, int colNum,
        string relOp, string queVal);

int main(int argc, char *argv[])
{
    if(argc == 2)
    {
        ifstream        infile(argv[1]);  //file identifier
        StringTable     table(infile);    //object creation
        vector<int>     vectInt;          //stores the columns to be displayed
        int             QcolNumber,       //column number for query
                        column;           //column number to be printed out
        string          relOp,            //holds relational operator entered
                        queVal;           //holds the query value entered
    
        cout << "Enter query column number (0 for first): ";
        cin >> QcolNumber;
        cout << "Enter relational operator (like C++): ";
        cin >> relOp;
        cout << "Enter query Value (ending in #): ";
        readMultiWord(queVal, cin);
        cout << "Column number to display first: ";
        cin >> column;
        while(column != -1)
        {
            vectInt.push_back(column);
        
            cout << "Enter column number to display next (-1 to quit): ";
            cin >> column;
        }
    
        compareRelOp(table, vectInt, QcolNumber, relOp, queVal);
    }
    else
        cout << "Proper program usage: ./guide file name\n";
    
    return 0;
}


/****************************************************************************
 * this function accepts 5 parameters and uses those parameters to determine
 * the outcome of the specified relational operator specified by the user
 * and the query value, also as specified by the user. When a match is found
 * while searching the 'table' the function 'displaySelected()' is called
 * to print out the specified elements in the row, as specified by the user.
 ****************************************************************************/

void compareRelOp(StringTable &table, vector<int> &vectInt, int colNum,
        string relOp, string queVal)
{
    int size = table.rows();  //holds number of rows in table
    
    for(int index = 0; index < size; index++)
    {
        if(relOp == "==")
        {
            if(table[index][colNum] == queVal)
                displaySelected(table[index], vectInt);
        }
        else if(relOp == "!=")
        {
            if(table[index][colNum] != queVal)
                displaySelected(table[index], vectInt);
        }
        else if(relOp == "<")
        {
            if(stringToFloat(queVal) < stringToFloat(table[index][colNum]))
                displaySelected(table[index], vectInt);
        }
        else if(relOp == ">")
        {
            if(stringToFloat(queVal) > stringToFloat(table[index][colNum]))
                displaySelected(table[index], vectInt);
        }
    }
}

